﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolERP_BLL;
using CoolERP_Entities;
using System.Data;

namespace CoolERP_Web.User
{
    public partial class ZL_CP_Edit : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                InitAllList();
            }
        }

        private void InitAllList()
        {
            #region 下拉框初始化
            BindCPX(this.ddlCPXBH, "请选择");
            BindCPLX(this.ddlCPLXBH, "请选择");
            BindJBDW(this.ddlCPDWBH, "9", "请选择");
            BindCPYSLX(this.ddlCPZYSLXBH, "请选择");
            BindCPYSLX(this.ddlCPPYSLXBH, "请选择");
            // 产品特性
            using (CS_CPTXBLL cBLL = new CS_CPTXBLL())
            {
                this.rbCPTX.DataSource = cBLL.GetAll();
                this.rbCPTX.DataValueField = "BH";
                this.rbCPTX.DataTextField = "MC";
                this.rbCPTX.DataBind();
                this.rbCPTX.SelectedIndex = 0;
            }
            #endregion

            #region=====修改时的下拉框选择修改======
            object id = this.GetUrlParam("id");
            if (id != null)
            {
                liTitle.Text = "修改";
                this.hdId.Value = id.ToString();
                ZL_CP entity = new ZL_CP();
                using (ZL_CPBLL cBLL = new ZL_CPBLL())
                {
                    entity = cBLL.GetEntity(t => t.ID == new Guid(this.hdId.Value));
                }

                if (this.ddlCPXBH.Items.FindByValue(entity.CPXBH) != null)
                {
                    this.ddlCPXBH.Items.FindByValue(entity.CPXBH).Selected = true;
                    BindCPXL(this.ddlCPXLBH, this.ddlCPXBH.SelectedValue, "请选择");
                    BindCPHXC(this.ddlCPMCBH, this.ddlCPXBH.SelectedValue, "请选择");
                }
                if (this.ddlCPLXBH.Items.FindByValue(entity.CPLXBH) != null)
                    this.ddlCPLXBH.Items.FindByValue(entity.CPLXBH).Selected = true;
                if (this.ddlCPXLBH.Items.FindByValue(entity.CPXLBH) != null)
                    this.ddlCPXLBH.Items.FindByValue(entity.CPXLBH).Selected = true;
                if (this.ddlCPMCBH.Items.FindByValue(entity.CPMCBH) != null)
                    this.ddlCPMCBH.Items.FindByValue(entity.CPMCBH).Selected = true;
                BindCPXH(this.ddlCPXHBH, this.ddlCPXBH.SelectedValue, this.ddlCPMCBH.SelectedValue, "请选择");
                this.ddlCPMC.Text = entity.CPMC;
                if (this.ddlCPXHBH.Items.FindByValue(entity.CPXHBH) != null)
                    this.ddlCPXHBH.Items.FindByValue(entity.CPXHBH).Selected = true;
                if (this.ddlCPDWBH.Items.FindByValue(entity.CPDWBH) != null)
                    this.ddlCPDWBH.Items.FindByValue(entity.CPDWBH).Selected = true;
                if (this.ddlCPZYSLXBH.Items.FindByValue(entity.CPZYSLXBH) != null)
                {
                    this.ddlCPZYSLXBH.Items.FindByValue(entity.CPZYSLXBH).Selected = true;
                    BindCPYS(this.ddlCPZYSBH, this.ddlCPZYSLXBH.SelectedValue, "请选择");
                }
                if (this.ddlCPZYSBH.Items.FindByValue(entity.CPZYSBH) != null)
                    this.ddlCPZYSBH.Items.FindByValue(entity.CPZYSBH).Selected = true;
                if (this.ddlCPPYSLXBH.Items.FindByValue(entity.CPPYSLXBH) != null)
                {
                    this.ddlCPPYSLXBH.Items.FindByValue(entity.CPPYSLXBH).Selected = true;
                    BindCPYS(this.ddlCPPYSBH, this.ddlCPPYSLXBH.SelectedValue, "请选择");
                }
                if (this.ddlCPPYSBH.Items.FindByValue(entity.CPPYSBH) != null)
                    this.ddlCPPYSBH.Items.FindByValue(entity.CPPYSBH).Selected = true;
                if (this.rbCPTX.Items.FindByValue(entity.CPTXBH) != null)
                    this.rbCPTX.Items.FindByValue(entity.CPTXBH).Selected = true;
                this.tbBH.Text = entity.BH;
                this.tbCPGG.Text = entity.CPGG;
                this.tbBZ.Text = entity.BZ;
                this.tbCPCBJ.Text = entity.CBJ == null ? "" : entity.CBJ.Value.ToString("F");
                this.tbCPPFJ.Text = entity.PFJ == null ? "" : entity.PFJ.Value.ToString("F");
                this.tbCPLSJ.Text = entity.LSJ == null ? "" : entity.LSJ.Value.ToString("F");
            }
            #endregion
        }

        #region DropDownList Changed
        protected void ddlCPXBH_SelectedIndexChanged(object sender, EventArgs e)
        {
            Literal1.Text = ddlCPXBH.SelectedValue.Trim();
            this.tbBH.Text = "";
            this.lbResult.Text = "";
            if (!string.IsNullOrEmpty(this.ddlCPXBH.SelectedValue))
            {
                BindCPXL(this.ddlCPXLBH, this.ddlCPXBH.SelectedValue, "请选择");
                BindCPHXC(this.ddlCPMCBH, this.ddlCPXBH.SelectedValue, "请选择");
                BindCPXH(this.ddlCPXHBH, this.ddlCPXBH.SelectedValue, this.ddlCPMCBH.SelectedValue, "请选择");
            }
        }
        protected void ddlCPLXBH_SelectedIndexChanged(object sender, EventArgs e)
        {
            Literal2.Text = ddlCPLXBH.SelectedValue.Trim();
        }
        protected void ddlCPXLBH_SelectedIndexChanged(object sender, EventArgs e)
        {
            Literal3.Text = ddlCPXLBH.SelectedValue.Trim();
        }
        protected void ddlCPMCBH_SelectedIndexChanged(object sender, EventArgs e)
        {
            Literal4.Text = ddlCPMCBH.SelectedValue.Trim();

            this.tbBH.Text = "";
            this.lbResult.Text = "";
            if (!string.IsNullOrEmpty( this.ddlCPMCBH.SelectedValue))
                BindCPXH(this.ddlCPXHBH, this.ddlCPXBH.SelectedValue, this.ddlCPMCBH.SelectedValue, "请选择");
        }
        protected void ddlCPXHBH_SelectedIndexChanged(object sender, EventArgs e)
        {
            Literal5.Text = ddlCPXHBH.SelectedValue.Trim();
        }
        protected void ddlCPZYSLXBH_SelectedIndexChanged(object sender, EventArgs e)
        {
            Literal6.Text = ddlCPZYSLXBH.SelectedValue.Trim();
            this.tbBH.Text = "";
            this.lbResult.Text = "";
            BindCPYS(this.ddlCPZYSBH, this.ddlCPZYSLXBH.SelectedValue, "请选择");
        }
        protected void ddlCPZYSBH_SelectedIndexChanged(object sender, EventArgs e)
        {
            Literal7.Text = ddlCPZYSBH.SelectedValue.Trim();
            this.tbBH.Text = "";
            this.lbResult.Text = "";
        }
        protected void ddlCPPYSLXBH_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindCPYS(this.ddlCPPYSBH, this.ddlCPPYSLXBH.SelectedValue, "请选择");
        }
        protected void rbCPTX_SelectedIndexChanged(object sender, EventArgs e)
        {
            Literal8.Text = rbCPTX.SelectedValue.Trim();
        }
        #endregion

        private void ClearControlsValue()
        {
            this.lbResult.Text = "";
            this.tbBH.Text = "";
            this.tbBZ.Text = "";
            this.tbCPGG.Text = "";
            this.tbCPCBJ.Text = "";
            this.tbCPLSJ.Text = "";
            this.tbCPPFJ.Text = "";
            this.ddlCPDWBH.SelectedValue = "";
            this.ddlCPMCBH.SelectedValue = "";
            this.ddlCPMC.Text = "";
            this.ddlCPXHBH.SelectedValue = "";
            this.ddlCPXLBH.SelectedValue = "";
            this.ddlCPZYSLXBH.SelectedValue = "";
            this.ddlCPZYSBH.SelectedValue = "";
            this.ddlCPPYSLXBH.SelectedValue = "";
            this.ddlCPPYSBH.SelectedValue = "";
            this.rbCPTX.SelectedIndex = 0;
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            this.lbResult.Text = "";

            //  产品编码：产品编码按新的总规划思路来设计，15位，第1位为数字3。分厂-先定01
            //  家具成品("3")	分厂("01")	产品线(1)	产品类型(1)	产品名称(2)	产品系列(2)	产品型号(2)	主颜色(3) 特性(1) "-"	二级部件名称代码(3)	工艺特征(1)	三级部件或流水码(2)				
            string cpx = ddlCPXBH.SelectedValue.Trim();
            string cplx = ddlCPLXBH.SelectedValue.Trim();
            string cpmc = ddlCPMCBH.SelectedValue.Trim();
            string cpxl = ddlCPXLBH.SelectedValue.Trim();
            string cpxh = ddlCPXHBH.SelectedValue.Trim();
            string zyslx = ddlCPZYSLXBH.SelectedValue.Trim();
            string zys = ddlCPZYSBH.SelectedValue.Trim();
            string tx = rbCPTX.SelectedValue.Trim();
            if (string.IsNullOrEmpty(cpx))
            {
                this.lbResult.Text = "请选择产品线";
                return;
            }
            if (string.IsNullOrEmpty(cplx))
            {
                this.lbResult.Text = "请选择产品类型";
                return;
            }
            if (string.IsNullOrEmpty(cpxl))
            {
                this.lbResult.Text = "请选择产品系列";
                return;
            }
            if (string.IsNullOrEmpty(cpmc))
            {
                this.lbResult.Text = "请选择产品产品核心词";
                return;
            }
            if (string.IsNullOrEmpty(cpxh))
            {
                this.lbResult.Text = "请选择产品型号";
                return;
            }
            if (string.IsNullOrEmpty(zyslx))
            {
                this.lbResult.Text = "请选择产品主色类型";
                return;
            }
            if (string.IsNullOrEmpty(zys))
            {
                this.lbResult.Text = "请选择产品主色";
                return;
            }
            string bh = "3" + Utility.FactoryBH + cpx + cplx + cpmc + cpxl + cpxh + zyslx + zys + tx;
            string lsh = "0000";
            using (ZL_CPBLL cBLL = new ZL_CPBLL())
            {
                if (ddlCPLXBH.SelectedValue != "1")//定制产品
                {
                    List<ZL_CP> items = cBLL.GetList(t => t.BH.StartsWith(bh));
                    if (items.Count > 0)
                    {
                        string tempbh = items.Select(t => t.BH).Max();
                        tempbh = tempbh.Substring(tempbh.LastIndexOf('-') + 1).TrimStart('0');
                        lsh = (Convert.ToInt32(tempbh) + 1).ToString().PadLeft('0');
                    }
                    else
                        lsh = "0001";
                }
                else//常规产品
                {
                    string temp = bh + "-" + lsh;
                    if (cBLL.IsExist(t => t.BH == temp))
                    {
                        this.lbResult.Text = "已存在此编号，请重新定义";
                        return;
                    }
                }
            }
            bh += "-" + lsh;
            this.tbBH.Text = bh;
        }

        protected void btnSave_Click(object sender, ImageClickEventArgs e)
        {
            this.lbResult.Text = "";
            if (string.IsNullOrEmpty(this.tbBH.Text.Trim()))
            {
                this.lbResult.Text = "请生成编号";
                return;
            }
            if (string.IsNullOrEmpty( this.ddlCPXBH.SelectedValue))
            {
                this.lbResult.Text = "请选择产品线";
                return;
            }
            if (string.IsNullOrEmpty(this.ddlCPLXBH.SelectedValue))
            {
                this.lbResult.Text = "请选择产品类型";
                return;
            }
            if (string.IsNullOrEmpty(this.ddlCPMCBH.SelectedValue))
            {
                this.lbResult.Text = "请选择产品核心词";
                return;
            }
            if (string.IsNullOrEmpty(this.ddlCPXLBH.SelectedValue))
            {
                this.lbResult.Text = "请选择产品系列";
                return;
            }
            if (string.IsNullOrEmpty(this.ddlCPXHBH.SelectedValue))
            {
                this.lbResult.Text = "请选择产品型号";
                return;
            }
            if (string.IsNullOrEmpty(this.ddlCPZYSLXBH.SelectedValue))
            {
                this.lbResult.Text = "请选择产品主颜色类型";
                return;
            }
            if (string.IsNullOrEmpty(this.ddlCPZYSBH.SelectedValue))
            {
                this.lbResult.Text = "请选择产品主颜色";
                return;
            }
            if (string.IsNullOrEmpty(tbCPCBJ.Text.ToString().Trim()))
            {
                this.lbResult.Text = "请输入产品成本价";
                return;
            }

            using (ZL_CPBLL cBLL = new ZL_CPBLL())
            {
                ZL_CP entity = new ZL_CP();
                if (!string.IsNullOrEmpty(this.hdId.Value))
                    entity = cBLL.GetEntity(t => t.ID == new Guid(this.hdId.Value));
                else
                    entity.ID = Guid.NewGuid();
                entity.BH = this.tbBH.Text.Trim();
                entity.CPXBH = this.ddlCPXBH.SelectedValue;
                entity.CPLXBH = this.ddlCPLXBH.SelectedValue;
                entity.CPMCBH = this.ddlCPMCBH.SelectedValue;
                entity.CPMC = this.ddlCPMC.Text.Trim();
                entity.CPXLBH = this.ddlCPXLBH.SelectedValue;
                entity.CPXHBH = this.ddlCPXHBH.SelectedValue;
                entity.CPGG = this.tbCPGG.Text;
                entity.CPDWBH = this.ddlCPDWBH.SelectedValue;
                entity.CPZYSLXBH = this.ddlCPZYSLXBH.SelectedValue;
                entity.CPZYSBH = this.ddlCPZYSBH.SelectedValue;
                entity.CPPYSLXBH = this.ddlCPPYSLXBH.SelectedValue;
                entity.CPPYSBH = this.ddlCPPYSBH.SelectedValue;
                entity.CPTXBH = this.rbCPTX.SelectedValue;
                entity.CBJ = string.IsNullOrEmpty(this.tbCPCBJ.Text.Trim()) ? 0 : Convert.ToDouble(this.tbCPCBJ.Text.Trim());
                entity.PFJ = string.IsNullOrEmpty(this.tbCPPFJ.Text.Trim()) ? 0 : Convert.ToDouble(this.tbCPPFJ.Text.Trim());
                entity.LSJ = string.IsNullOrEmpty(this.tbCPLSJ.Text.Trim()) ? 0 : Convert.ToDouble(this.tbCPLSJ.Text.Trim());
                entity.BZ = this.tbBZ.Text;
                string resultMessage = "";

                try
                {
                    bool result = false;
                    if (string.IsNullOrEmpty(this.hdId.Value))
                        result = cBLL.Insert(entity);
                    else
                        result = cBLL.Update(entity);
                    if (result)
                    {
                        resultMessage = "保存成功";
                        //ClearControlsValue();
                    }
                }
                catch (Exception ex)
                {
                    resultMessage = "保存失败" + ex.Message;
                }
                finally
                {
                    this.lbResult.Text = resultMessage;
                }
            }
        }
    }
}